/*******************************************
 * Copyright 2007 Aphrodite Framework for javaME .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * Email : owner@aphroditeme.dev.java.net
 * Contributors:
 *     Kent Elton - initial API and implementation
 *******************************************/

package com.aphrodite.ui.bg;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Renders the a tiled image background
 * 
 * @author kentelt
 * 
 */
public class TiledImageBackground extends Background {

	private Image tile;
	private int tileHeight;
	private int tileWidth;
	private int totalH_Tiles;
	private int totalV_Tiles;

	public TiledImageBackground(Image tile) {
		super();
		this.tile = tile;
		setDims();
	}

	public void cleanUp() {
		tile = null;

	}

	public void render(Graphics g) {
		int tempX = x;
		int tempY = y;
		for (int i = 0; i < totalV_Tiles; i++) {
			for (int j = 0; j < totalH_Tiles; j++) {
				g.drawImage(tile, tempX, tempY, 0);
				tempX += tileWidth;
			}
			tempX = x;
			tempY += tileHeight;
		}
	}

	private void setDims() {
		this.tileHeight = this.tile.getHeight();
		this.tileWidth = this.tile.getWidth();
		this.totalH_Tiles = width / tileWidth + 1;
		this.totalV_Tiles = height / tileHeight + 1;
	}

	public void setHeight(int height) {
		this.height = height;
		setDims();
	}

	public void setTile(Image tile) {
		this.tile = tile;
		setDims();
	}

	public void setWidth(int width) {
		this.width = width;
		setDims();
	}
}
